package ws.jdbc.concurrent.driver.sqlserver

import java.sql.*;

/**
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
class CConnectionTest extends GroovyTestCase {

  void testGetPropertiesFromUrl() {
    println "testGetPropertiesFromUrl()"

    // [0] parameter, [1] expected, [2] expected, [3] expected
    def parameters = [
      ["", "", "", ""],
      ["...;", "", "", ""],
      ["...;username=user", "user", "", ""],
      ["...;password=pwd", "", "pwd", ""],
      ["...;database=db", "", "", "db"],
      ["...;UserNamE=user", "user", "", ""],
      ["...;PassWorD=pwd", "", "pwd", ""],
      ["...;DataBasE=db", "", "", "db"],
      ["...;username=user;password=pwd;database=db", "user", "pwd", "db"],
    ]
    parameters.each {
      def Connection con = new CConnection()
      con.getPropertiesFromUrl(it[0])
      assertEquals it[1], con.userName
      assertEquals it[2], con.password
      assertEquals it[3], con.database
    }
  }

  void testParseUrl() {
    println "testParseUrl()"
    
    def Connection con = new CConnection()
    def parameters = [
      ["jdbc:sqlserver://srvname", null],
      ["jdbc:sqlserver://srvname\\instance", null],
      ["jdbc:sqlserver://srvname\\instance:8989", null]
    ]
    def expected = [
      ["srvname", 1433, ""],
      ["srvname", 1433, "instance"],
      ["srvname", 8989, "instance"],
    ]

    parameters.eachWithIndex{param, i ->
      /* test method. */
      con.parseUrl param[0], param[1]

      /* test expectations. */
      assertEquals expected[i][0], con.serverName
      assertEquals expected[i][1], con.portNumber
      assertEquals expected[i][2], con.instanceName
    }
  }

  void testCreateInstance() throws SQLException {
    println "testCreateInstance()"
    
    Properties loginInfo = new Properties()
    assertNotNull loginInfo
    loginInfo.put("username", "sa");
    loginInfo.put("password", "sg4765j");
    loginInfo.put("database", "JDBCDriverTests");
    assertNotNull new CConnection("jdbc:sqlserver://goldberg-wm:1433", loginInfo)
  }
}

